package com.md.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.model.StorageClass;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.md.api.common.utils.AliOssUtil;
import com.md.api.common.utils.HttpUtil;
import com.md.api.common.utils.JsonUtils;
import com.md.api.common.utils.RedisUtil;
import com.md.api.entity.HttpResult;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.entity.ftp.Account;
import com.md.api.entity.ftp.FtpFileInfo;
import com.md.api.entity.ftp.Modules;
import com.md.api.service.UserSevice;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("/test")
@RestController
public class TestController {


	@Resource
	RedisUtil redisUtil;
	@Resource
	UserSevice userSevice;
	@Autowired
	RedissonClient redisson;

	@RequestMapping("/init")
	public HttpResult init(@RequestBody ComposeQueryDto dto) {
		String key = "product_" + dto.getSpuId();
		redisUtil.set(key, dto.getStatus());
		return HttpResult.OK;
	}


	@RequestMapping("/jmiter")
	public HttpResult buy(@RequestBody ComposeQueryDto dto) {
		String lock_key = "product_lock_" + dto.getSpuId();
		String key = "product_" + dto.getSpuId();
		RLock lock = redisson.getLock(lock_key);
		//try catch 设计是为了防止在执行业务的时候出现异常导致redis锁一直无法释放
		try {
			lock.lock();
			int count = Integer.parseInt(redisUtil.get(key).toString());
			if (count > 0) {
				redisUtil.set(key, --count);
				System.out.println(key + "商品购买成功，剩余库存" + count);
				return HttpResult.OK;
			}
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		return HttpResult.fail("暂无剩余");
	}


	@RequestMapping("/resetFtpFile")
	public HttpResult resetFtpFile() {
		try {
			userSevice.resetFtpFile();
			return HttpResult.fail("resetFtpFile success");
		} catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("resetFtpFile failed");
		}
	}


	@RequestMapping("/coldFile")
	public HttpResult coldFile() {
		try {
			AliOssUtil.resetStorageClass("https://photo-oss-file.oss-cn-shanghai.aliyuncs.com/5737727125954d453f6af30a14e5eeec/BJ1712131773563.png",StorageClass.ColdArchive);
			return HttpResult.fail("coldFile success");
		} catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("coldFile failed");
		}
	}

	@RequestMapping("/thawingFile")
	public HttpResult thawingFile() {
		try {
			AliOssUtil.thawingColdFile("https://photo-oss-file.oss-cn-shanghai.aliyuncs.com/5737727125954d453f6af30a14e5eeec/BJ1712131885172.png");
			AliOssUtil.resetStorageClass("https://photo-oss-file.oss-cn-shanghai.aliyuncs.com/5737727125954d453f6af30a14e5eeec/BJ1712131885172.png",StorageClass.Standard);
			return HttpResult.fail("thawingFile success");
		} catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("coldFile failed");
		}
	}

	@RequestMapping("/getThawingStatus")
	public HttpResult getThawingStatus() {
		try {
			AliOssUtil.getThawingStatus("https://photo-oss-file.oss-cn-shanghai.aliyuncs.com/5737727125954d453f6af30a14e5eeec/BJ1712131885172.png");
			return HttpResult.fail("getThawingStatus success");
		} catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("coldFile failed");
		}
	}

	@RequestMapping("/setLifecycle")
	public HttpResult setLifecycle() {
		try {
			AliOssUtil.setLifecycle("IARule","5737727125954d453f6af30a14e5eeec",90, StorageClass.IA);
			return HttpResult.fail("setLifecycle success");
		} catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("setLifecycle failed");
		}
	}


}
